package com.fesine.quartz.chapter6;

import com.fesine.quartz.chapter4.PrintJob;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

import java.text.ParseException;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/11/19 15:26
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/19 15:26
 */
public class SchedulerMain {
    static Log logger = LogFactory.getLog(SchedulerMain.class);

    public static void main(String[] args) {
        new SchedulerMain().startScheduler();
    }

    public void startScheduler() {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            logger.info("scheduler starting up...");
            //scheduler.start();
            //schedulerJob(scheduler, PrintJob.class);
            unSchedulerJOb(scheduler, PrintJob.class);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    private void schedulerJob(Scheduler scheduler,Class jobClass) {
        JobDetail jobDetail = new JobDetail(jobClass.getSimpleName(), Scheduler.DEFAULT_GROUP, jobClass);
        try {
            CronTrigger trigger = new CronTrigger("cronTrigger", Scheduler.DEFAULT_GROUP, "0/10 * * ?" +
                    " * *");
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    private void unSchedulerJOb(Scheduler scheduler, Class jobClass) {
        try {
            scheduler.deleteJob(jobClass.getSimpleName(), Scheduler.DEFAULT_GROUP);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
